// @author Tjzlo
// @version 2025/2/6 01:48

package com.old.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.old.dto.CommentDTO;
import com.old.dto.CommentQueryDTO;
import com.old.entity.Comment;
import com.old.mapper.CommentMapper;
import com.old.result.PageBean;
import com.old.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CommentServiceImpl implements CommentService {

    private final CommentMapper commentMapper;
    /**
     * 添加评论
     * @param commentDTO
     */
    @Transactional
    public void addComment(CommentDTO commentDTO) {
        commentMapper.save(commentDTO);
    }

    /**
     * 分页查询评论
     * @param commentQueryDTO
     * @return
     */
    public PageBean commentListPage(CommentQueryDTO commentQueryDTO) {
        //分页查询
        PageHelper.startPage(commentQueryDTO.getPage(), commentQueryDTO.getPageSize());
        Page<Comment> page = commentMapper.listPage(commentQueryDTO);

        long total = page.getTotal();
        List<Comment> records = page.getResult();

        return new PageBean(total, records);
    }
}
